package com.itkaien.service.impl;

import com.github.pagehelper.PageInfo;
import com.itkaien.dto.GoodLedgetDto;
import com.itkaien.mapper.LedgerMapper;
import com.itkaien.pojo.Goods;
import com.itkaien.pojo.Ledger;
import com.itkaien.service.GoodsService;
import com.itkaien.service.LedgerService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LedgerServiceImpl implements LedgerService {

    @Autowired
    private LedgerMapper ledgerMapper;

    @Autowired
    private GoodsService goodsService;

    /**
     * 插入一条流水数据
     * @param ledger
     */
    @Override
    public void addLedger(Ledger ledger) {
        ledgerMapper.insert(ledger);
    }

    @Override
    public PageInfo<GoodLedgetDto> getpageAllledger(Integer goodsId, Integer start, Integer pageSize) {

        PageInfo<GoodLedgetDto> pageInfo = new PageInfo<>();
        List<Ledger> ledgers = ledgerMapper.selectpageAllledger(goodsId, start, pageSize);

       List<GoodLedgetDto> goodLedgetDtos = new ArrayList<>();

        Goods goodsname = goodsService.selectoneGood(goodsId);


        for (Ledger ledger1:ledgers) {
            GoodLedgetDto goodLedgetdto = new GoodLedgetDto();
            goodLedgetdto.setGoodsId(ledger1.getGoodsId());
            goodLedgetdto.setGoodsName(goodsname.getName());
            goodLedgetdto.setCreateTime(ledger1.getCreateTime());
            goodLedgetdto.setStoreId(ledger1.getStoreId());
            goodLedgetdto.setChangenum(ledger1.getChangenum());

            /**
             * 因为流水表缺少商品名字所以 创建一个公共类并抽取 出来最后赋值返回一个集合给前端带商品名字的
             */
            goodLedgetDtos.add(goodLedgetdto);
        }
        pageInfo.setList(goodLedgetDtos);
        pageInfo.setTotal(goodLedgetDtos.size());
        pageInfo.setPages(start);
        pageInfo.setPageSize(pageSize);
        return pageInfo;
    }
}
